﻿var actionUrl = 'handler/StaffAdminHandler.ashx';
var formUrl = "TestModules/html/RepairItemForm.htm";

$(function () {
    var size = { width: $(window).width(), height: $(window).height() };
    mylayout.init(size);

    autoResize({ dataGrid: '#staffGird', gridType: 'datagrid', callback: mygrid.databind, height: 36, width: 230 });

    $('#btnAddStaff').click(StaffAdminMethod.AddStaff); //新增报修（物品）
    $('#btnEditStaff').click(StaffAdminMethod.EditStaff); //修改报修（物品）
    $('#btnRefresh').click(function () { //刷新
        mygrid.reload();
    });

    $(window).resize(function () {
        size = { width: $(window).width(), height: $(window).height() };
        mylayout.resize(size);
    });
});

var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#layout').layout('panel', 'center');
        center.panel({
            onResize: function (w, h) {
                $('#staffGird').datagrid('resize', { width: w - 6, height: h - 36 });
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};

var mygrid = {
    databind: function (size) {
        $('#staffGird').datagrid({
            url: "handler/StaffAdminHandler.ashx",
            title: "维修记录列表",
            loadMsg: "正在加载等待维修数据，请稍等...",
            width: size.width,
            height: size.height,
            idField: 'Id',
            singleSelect: true,
            striped: true,
            rownumbers: true,
            columns: [[
                    { title: '主键', field: 'Id', hidden: true, align: "center" },
                    { title: '工单号', field: 'Code', align: "center", width: 100 },
                    { title: '电脑编号', field: 'Code', align: "center", width: 100 },
                    { title: '报修时间', field: 'Mobile', width: 120, align: "center" },
                    { title: '维修状态', field: 'OfficePhone', width: 120, align: "center" },
                    { title: '最后维修时间', field: 'OfficePhone', width: 150, align: "center" },
                    { title: '用户操作', field: 'OfficePhone', width: 120, align: "center" },
                    { title: 'UserId', field: 'UserId', hidden: true }
            ]],
            rowStyler: function (index, row, css) {
                if (row.UserId != "") {
                    return 'font-weight:bold;';
                }
            },
            onLoadSuccess: function (data) {
                if (data.rows.length > 0) {
                    $('#staffGird').datagrid("selectRow", 0);
                }
            }
        });
    },



    reload: function () {
        $('#staffGird').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#staffGird').datagrid('getSelected');
    }



};

var imgcheckbox = function (cellvalue, options, rowObject) {
    return cellvalue ? '<img src="/css/icon/ok.png" alt="正常" title="正常" />' : '<img src="/css/icon/stop.png" alt="禁用" title="禁用" />';
};

var StaffAdminMethod = {
    initData: function (organizeId) {
        top.$('#txt_Education,#txt_Degree,#txt_Title,#txt_TitleLevel,#txt_WorkingProperty,#txt_Party,#txt_Gender').combobox({ panelHeight: 'auto' });
        top.$('#txt_Birthday,#txt_TitleDate,#txt_WorkingDate,#txt_DimissionDate,#txt_JoinInDate').datebox({
            formatter: function (date) {
                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            },
            arser: function (date) {
                return new Date(Date.parse(date.replace(/-/g, "/")));
            }
        });

        var _organizeId = organizeId || 0;
        top.$('#txt_OrganizeId').combotree({
            data: organizeTree.data(),
            valueField: 'id',
            textField: 'text',
            value: _organizeId
        });
        //绑定各数据字典
        pubMethod.bindCategory('txt_Gender', 'Gender');
        pubMethod.bindCategory('txt_Education', 'Education');
        pubMethod.bindCategory('txt_WorkingProperty', 'WorkingProperty');
        pubMethod.bindCategory('txt_Degree', 'Degree');
        pubMethod.bindCategory('txt_Gender', 'Gender');
        pubMethod.bindCategory('txt_Title', 'Title');
        pubMethod.bindCategory('txt_TitleLevel', 'TitleLevel');
        pubMethod.bindCategory('txt_Nationality', 'Nationality');
        pubMethod.bindCategory('txt_Party', 'PoliticalStatus')
        /*
        top.$('#txt_role').combobox({ multiple: true, valueField: 'KeyId', textField: 'RoleName' });
        $.getJSON(actionUrl + '?' + createParam('roles'), function (d) {
        top.$('#txt_role').combobox({ data: d });
        (function (roles) {
        $.each(roles, function (i, n) {
        if (n.IsDefault == 1)
        top.$('#txt_role').combobox('setValue', n.KeyId);
        });
        } (d));
        });
        */
        top.$('#RepairTab').tabs({
            onSelect: function () {
                top.$('.validatebox-tip').remove();
            }
        });
        top.$('#txt_passSalt').val(randomString());

    },
    AddStaff: function () { //增加报修单（物品）
        if ($(this).linkbutton('options').disabled == true) {
            return;
        }
        //功能代码逻辑...
        var addDialog = top.$.hDialog({
            href: formUrl + '?v=' + Math.random(),
            width: 680,
            height: 500,
            title: '新增报修单（物品）',
            iconCls: 'icon-vcard_add',
            onLoad: function () {
                var dep = $('#organizeTree').tree('getSelected');
                var depID = 0;
                if (dep) {
                    depID = dep.id || 0;
                };
                top.$('#chk_Enabled').attr("checked", true);
                //如果左侧有选中组织机构，则添加的时候，部门默认选中
                StaffAdminMethod.initData(depID);
            },
            closed: false,
            submit: function () {
                var tab = top.$('#RepairTab').tabs('getSelected');
                var index = top.$('#RepairTab').tabs('getTabIndex', tab);
                if (top.$('#uiform').form('validate')) {
                    //var query = createParam('add', 0) + '&roles=' + top.$('#txt_role').combo('getValues');
                    var vOrganizeId = top.$('#txt_OrganizeId').combobox('getValue');
                    var query = 'action=AddStaff&vOrganizeId=' + vOrganizeId + '&' + top.$('#uiform').serialize();

                    $.ajaxjson(actionUrl, query, function (d) {
                        if (d.Success) {
                            msg.ok('添加成功');
                            mygrid.reload();
                            addDialog.dialog('close');
                        } else {
                            if (d.Data == -2) {
                                msg.error('用户名已存在，请更改用户名。');
                                if (index > 0)
                                    top.$('#RepairTab').tabs('select', 0);
                                top.$('#txt_username').select();
                            } else {
                                MessageOrRedirect(d);
                            }
                        }
                    });
                } else {
                    if (index > 0)
                        top.$('#RepairTab').tabs('select', 0);
                }
            }
        });
    },
    EditStaff: function () { //编辑物品（报修）
        if ($(this).linkbutton('options').disabled == true) {
            return;
        }
        //功能代码逻辑...
        var row = mygrid.selectRow();
        if (row) {
            var editDailog = top.$.hDialog({
                href: formUrl + '?v=' + Math.random(),
                title: '修改报修单（物品）',
                iconCls: 'icon-vcard_edit',
                width: 680,
                height: 500,
                onLoad: function () {
                    var dep = $('#organizeTree').tree('getSelected');
                    var depID = 0;
                    if (dep)
                        depID = dep.id || 0;

                    StaffAdminMethod.initData(depID);
                    var parm = 'action=GetEntity&KeyId=' + row.Id;
                    $.ajaxjson(actionUrl, parm, function (data) {
                        if (data) {
                            //OrganizeId
                            top.$('#txt_Code').val(data.Code);
                            top.$('#txt_RealName').val(data.RealName);
                            top.$('#txt_Birthday').datebox('setValue', data.Birthday);
                            top.$('#txt_Gender').combobox('setValue', data.Gender);
                            top.$('#txt_Age').val(data.Age);
                            top.$('#txt_Major').val(data.Major);
                            top.$('#txt_School').val(data.School);
                            top.$('#txt_Education').combobox('setValue', data.Education);
                            top.$('#txt_Degree').combobox('setValue', data.Degree);
                            top.$('#txt_Title').combobox('setValue', data.Title);
                            top.$('#txt_TitleLevel').combobox('setValue', data.TitleLevel);
                            top.$('#txt_TitleDate').datebox('setValue', data.TitleDate);
                            top.$('#txt_WorkingProperty').combobox('setValue', data.WorkingProperty);
                            top.$('#txt_WorkingDate').datebox('setValue', data.WorkingDate);
                            top.$('#txt_IdentificationCode').val(data.IdentificationCode);
                            top.$('#txt_BankCode').val(data.BankCode);
                            top.$('#txt_JoinInDate').datebox('setValue', data.JoinInDate);
                            top.$('#txt_Email').val(data.Email);
                            top.$('#txt_Mobile').val(data.Mobile);
                            top.$('#txt_ShortNumber').val(data.ShortNumber);
                            top.$('#txt_QICQ').val(data.QICQ);
                            top.$('#txt_OfficeZipCode').val(data.OfficeZipCode);
                            top.$('#txt_OfficePhone').val(data.OfficePhone);
                            top.$('#txt_OfficeFax').val(data.OfficeFax);
                            top.$('#txt_OfficeAddress').val(data.OfficeAddress);
                            top.$('#txt_NativePlace').val(data.NativePlace);
                            top.$('#txt_HomeZipCode').val(data.HomeZipCode);
                            top.$('#txt_HomeFax').val(data.HomeFax);
                            top.$('#txt_Party').combobox('setValue', data.Party);
                            top.$('#txt_Nation').val(data.Nation);
                            top.$('#txt_Nationality').val(data.Nationality);
                            top.$('#txt_HomePhone').val(data.HomePhone);
                            top.$('#txt_Telphone').val(data.Telphone);
                            top.$('#txt_HomeAddress').val(data.HomeAddress);
                            top.$('#txt_DimissionDate').datebox('setValue', data.DimissionDate);
                            top.$('#txt_DimissionWhither').val(data.DimissionWhither);
                            top.$('#chk_IsDimission').attr('checked', data.IsDimission == "1");
                            top.$('#txt_DimissionCause').val(data.DimissionCause);
                            top.$('#chk_Enabled').attr('checked', data.Enabled == "1");
                            top.$('#txt_Description').val(data.Description);
                        }
                    });

                    //                    top.$('#txt_RealName').val(row.RealName);
                    //                    top.$('#txt_Code').val(row.Code);
                    //                    top.$('#txt_Birthday').val(row.Birthday);
                    //                    top.$('#txt_Gender').combobox('setValue', row.Gender);
                    //                    top.$('#chk_Enabled').attr('checked', row.Enabled == "1");
                    //                    top.$('#txt_Description').val(row.Description);
                },
                submit: function () {
                    if (top.$('#uiform').validate().form()) {
                        var vOrganizeId = top.$('#txt_OrganizeId').combobox('getValue');
                        var query = 'action=EditStaff&vOrganizeId=' + vOrganizeId + '&KeyId=' + row.Id + '&' + top.$('#uiform').serialize();
                        $.ajaxjson(actionUrl, query, function (d) {
                            if (d.Success) {
                                msg.ok(d.Message);
                                editDailog.dialog('close');
                                mygrid.reload();
                            } else {
                                MessageOrRedirect(d);
                            }
                        });
                    }
                }
            });
        } else {
            msg.warning('请选择待修改的物品（报修单）!');
            return false;
        }
        return false;
    },
    DeleteStaff: function () { //删除员工（职员）
        if ($(this).linkbutton('options').disabled == true) {
            return;
        }
        //功能代码逻辑...
        var row = mygrid.selectRow();
        if (row != null) {
            var query = 'action=DeleteStaff&KeyId=' + row.Id;
            $.messager.confirm('询问提示', '确定要删除选中的员工（职员）吗？', function (data) {
                if (data) {
                    $.ajaxjson(actionUrl, query, function (d) {
                        if (d.Success) {
                            msg.ok(d.Message);
                            mygrid.reload();
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
                else {
                    return false;
                }
            });
        }
        else {
            msg.warning('请选择要删除的操作权限项!');
            return false;
        }
        return false;
    },
    MoveTo: function () { //移动员工（职员）
        if ($(this).linkbutton('options').disabled == true) {
            return;
        }
        //功能代码逻辑...

    },
    Export: function () { //导出员工（职员）
        if ($(this).linkbutton('options').disabled == true) {
            return;
        }
        //功能代码逻辑...

    }
};

//公共方法
var pubMethod = {
    bindCategory: function (categoryControl, categoryCode) {
        if (categoryControl == '' || categoryCode == '') {
            return;
        }

        top.$('#' + categoryControl).combobox({
            url: 'Modules/handler/DataItemAdminHandler.ashx?action=GetCategory&categorycode=' + categoryCode,
            method: 'get',
            valueField: 'ItemValue',
            textField: 'ItemName',
            editable: false,
            panelHeight: 'auto'
        });
    }
}